home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / readfloat.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  139 lines

  1. /***********************************************************************
  2.  * $Id: readfloat.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *   Read a float from a file. Valid sepertors are SPACE, \t \n
  8.  *   # introduces a comment that ends at \n
  9.  *
  10.  *   The floating number must be of the form [+-]nnn.xxx, i.e.,
  11.  *   no e and E is permitted
  12.  *
  13.  *   bad_character is defined in readint.c
  14.  ***********************************************************************/
  15. #if !defined(lint) && defined(F_ID)
  16. char *id_rfloat = "$Id: readfloat.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>        /* for isdigit */
  21. #include "ulib.h"
  22.  
  23. #define IS_FS(c)      ( (c) == ' ' || (c) == '\t' || (c) == '\n')
  24. #define IS_COMMENT(c) ( (c) == '#')
  25.  
  26. extern int skip_comment(FILE * fp);
  27. extern void bad_character(int);
  28.  
  29. double
  30. readfloat(FILE * fp)
  31. {
  32.     register int c;
  33.     register double num1 = 0.0, num2 = 0.0;    /* must be in double */
  34.     register double sign = 1.0;
  35.  
  36.     do
  37.       {
  38.       c = getc(fp);
  39.       while (IS_COMMENT(c))
  40.           c = skip_comment(fp);
  41.       }
  42.     while (IS_FS(c));
  43.  
  44.     if (c == '-')
  45.       {
  46.       sign = -1.0;
  47.       c = getc(fp);
  48.       }
  49.     else if (c == '+')
  50.       {
  51.       sign = 1.0;
  52.       c = getc(fp);
  53.       }
  54.     if (!isdigit(c))
  55.       {
  56.       bad_character(c);
  57.       return -10000000.0;
  58.       }
  59.     do
  60.       {
  61.       num1 = 10.0 * num1 + c - '0';
  62.       c = getc(fp);
  63.       }
  64.     while (isdigit(c));
  65.  
  66.     /*
  67.      * the only permissible char at this point is . or FS. if we get an FS,
  68.      * we are done, else, read the fractional part
  69.      */
  70.  
  71.     if (IS_FS(c))
  72.     return (num1 * sign);
  73.     else if (c == '.')
  74.       {                /* ok, do the fractional part */
  75.       register double fact = 0.1;
  76.       while ((c = getc(fp)) != EOF && isdigit(c))
  77.         {
  78.         num2 = num2 + fact * (c - '0');
  79.         fact *= 0.1;
  80.         }
  81.       return ((num1 + num2) * sign);
  82.       }
  83.     else
  84.       {                /* error */
  85.       bad_character(c);
  86.       return -10000000.0;
  87.       }
  88. }
  89.  
  90.  
  91. /* Read a positive floating number. return EOF if error */
  92. double
  93. readpfloat(FILE * fp)
  94. {
  95.     register int c;
  96.     register double num1 = 0, num2 = 0;
  97.  
  98.     do
  99.       {
  100.       c = getc(fp);
  101.       while (IS_COMMENT(c))
  102.           c = skip_comment(fp);
  103.       }
  104.     while (IS_FS(c));
  105.  
  106.     if (!(c == '+' || isdigit(c)))
  107.       {
  108.       bad_character(c);
  109.       return EOF;
  110.       }
  111.     if (!isdigit(c))
  112.     c = getc(fp);
  113.     do
  114.       {
  115.       num1 = 10.0 * num1 + c - '0';
  116.       c = getc(fp);
  117.       }
  118.     while (isdigit(c));
  119.  
  120.  
  121.     if (IS_FS(c))        /* we are done */
  122.     return num1;
  123.     else if (c == '.')
  124.       {                /* read the fractional part */
  125.       register double fact = 0.1;
  126.       while ((c = getc(fp)) != EOF && isdigit(c))
  127.         {
  128.         num2 = num2 + fact * (c - '0');
  129.         fact *= 0.1;
  130.         }
  131.       return (num1 + num2);
  132.       }
  133.     else
  134.       {                /* error */
  135.       bad_character(c);
  136.       return EOF;
  137.       }
  138. }
  139.